﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Configuration;
using System.Data.SqlClient;
using NotificationLib;
using GalacticLib;

namespace Galactic
{
    public class Global : System.Web.HttpApplication
    {
        Config cfg;

        string GetConfigValue(SqlConnection conn, string ConfigKey)
        {
            return cfg.GetConfigValue(ConfigKey);
        }

        protected void Application_Start(object sender, EventArgs e)
        {

          //  #if (!DEBUG)
            Application.Add("ConnectionString", ConfigurationManager.ConnectionStrings["galactic"].ConnectionString);

            using(SqlConnection conn = new SqlConnection(Application["ConnectionString"].ToString()))            
            {
                cfg = new Config(conn);

                  Application.Add("PushoverApiKey", GetConfigValue(conn,"PushoverApiKey"));

                  PushoverNotificationConfig pnc = new PushoverNotificationConfig();
                  pnc.ApiKey = Application["PushoverApiKey"].ToString();
                  Application.Add("PushoverNotificationConfig", pnc);

                  EmailNotificationConfig enc = new EmailNotificationConfig();
                  enc.AuthenticationType = EmailNotificationAuthenticationType.UserAndPass;
                  enc.MailServer = GetConfigValue(conn, "MailServer");
                  enc.MailServerPort = short.Parse(GetConfigValue(conn, "MailPort"));
                  enc.FromAddress = new System.Net.Mail.MailAddress(GetConfigValue(conn, "MailFrom"), GetConfigValue(conn, "MailFromName"));
                  enc.Username = GetConfigValue(conn, "MailUser");
                  enc.Password = GetConfigValue(conn, "MailPassword");

                  Application.Add("EmailNotificationConfig", enc);                
                
            }
         //   #endif
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}